/**
 * 
 */
package com.nari.heatassist.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.nari.baseinfo.model.IHeatMeter;
import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.heatassist.model.IBuildingConsumeConfig;
import com.nari.heatassist.utils.BuildingConsumeConfigEntity;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.rescontroller.dao.IResourceAccessControlDao;

/**
 * 
 * @author Xiaopeng
 * @date 2014-4-22
 */
public class BuildingConsumeConfigDaoImpl extends GenericDaoImpl<IBuildingConsumeConfig, String>
		implements IBuildingConsumeConfigDao {
	private IResourceAccessControlDao iResourceAccessControlDao;
	
	/**
	 * 分页查询楼栋耗热配置项
	 * @param projectUID
	 * @param communityUID
	 * @param buildingUID
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<BuildingConsumeConfigEntity> queryConsumeConfigPage(String projectUID, 
			String communityUID, String buildingUID, PageCond pageCond) throws Exception {
		String hsql = "select bcc.projectID,bcc.communityID,bcc.buildingID,count(*) " +
				"from IBuildingConsumeConfig bcc";
		hsql += " where 1=1";
		//所属项目
		if(projectUID!=null && !"".equals(projectUID)) {
			hsql += " and bcc.projectID='"+projectUID+"'";
		}
		//所属小区
		if(communityUID!=null && !"".equals(communityUID)) {
			hsql += " and bcc.communityID='"+communityUID+"'";
		}
		//所属楼栋
		if(buildingUID!=null && !"".equals(buildingUID)) {
			hsql += " and bcc.buildingID='"+buildingUID+"'";
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
		if(!"".equals(authorityRes)) {
			hsql += " and bcc.projectID in("+authorityRes+")";
		}
		hsql += " group by bcc.buildingID";
		//结果集个数
		int total = this.getCurrentSession().createQuery(hsql).list().size();
		//结果集排序
		hsql += " order by bcc.buildingID";
		//结果集分页
		Query listQuery = this.getCurrentSession().createQuery(hsql);
		listQuery.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//封装查询结果
		List<Object> queryResultList = listQuery.list();
		List<BuildingConsumeConfigEntity> configList = new ArrayList<BuildingConsumeConfigEntity>();
		if(queryResultList!=null && queryResultList.size()>0) {
			for(Object queryResult: queryResultList) {
				Object[] resultArray = (Object[]) queryResult;
				projectUID = (String) resultArray[0];
				communityUID = (String) resultArray[1];
				buildingUID = (String) resultArray[2];
				int meterCounter = ((Long)resultArray[3]).intValue();
				BuildingConsumeConfigEntity configEntity = BuildingConsumeConfigEntity.getInstance(projectUID, 
						communityUID, buildingUID, meterCounter);
				configList.add(configEntity);
			}
		}
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<BuildingConsumeConfigEntity> consumeConfigPage = new ResultWithPageCond<BuildingConsumeConfigEntity>();
		consumeConfigPage.setList(configList);
		consumeConfigPage.setPageCond(pageCond);
		return consumeConfigPage;
	}
	
	/**
	 * 分页查询参与楼栋热计量的配置列表
	 * @param buildingUID
	 * @param meterNo
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IBuildingConsumeConfig> queryMeasureMeterPage(String buildingUID, String meterNo, 
			PageCond pageCond) throws Exception {
		String measureMeterQueryHql = "from IBuildingConsumeConfig bcc " +
				"where bcc.buildingID='"+buildingUID+"'";
		//热表号动态查询
		if(meterNo!=null && !"".equals(meterNo.trim())) {
			measureMeterQueryHql += " and lower(bcc.meterNo) like '%"+meterNo.trim().toLowerCase()+"%'";
		}
		//结果集个数查询
		String countQueryHql = "select count(*) "+measureMeterQueryHql;
		int total = ((Long)this.getCurrentSession().createQuery(countQueryHql).uniqueResult()).intValue();
		//结果集排序
		measureMeterQueryHql += " order by bcc.meterNo asc";
		//结果集分页
		Query listQuery = this.getCurrentSession().createQuery(measureMeterQueryHql);
		listQuery.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//封装查询结果
		pageCond.setTotal(total);
		ResultWithPageCond<IBuildingConsumeConfig> meterPage = new ResultWithPageCond<IBuildingConsumeConfig>();
		meterPage.setList(listQuery.list());
		meterPage.setPageCond(pageCond);
		return meterPage;
	}
	
	/**
	 * 通过楼栋信息删除楼栋耗热计量配置项
	 * @param buildingUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	public ResponseMsg deleteBuildingConsumeConfig(String buildingUID) throws Exception {
		String hsql = "delete from IBuildingConsumeConfig bcc " +
				"where bcc.buildingID='"+buildingUID+"'";
		this.runFormatHSql(hsql);
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 查询可用于楼栋耗热配置的热表列表，剔掉已参与该楼栋配置的热量表
	 * 根据楼栋与热表的物理关系(主干、分支等关系)，只要热表与楼栋在同一小区，即可进行逻辑关系配置
	 * @param projectUID
	 * @param communityUID
	 * @param meterNo
	 * @param buildingUID
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-24
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IHeatMeter> queryConfigMeasureMeterPage(String projectUID, 
			String communityUID, String meterNo, String buildingUID, 
			PageCond pageCond) throws Exception {
		//楼栋已配置热表
		String alreadyConfigHql = "select bcc.meterNo from IBuildingConsumeConfig bcc " +
				"where bcc.buildingID='"+buildingUID+"'";
		//楼栋可配置热表
		String possibleConfigHql = "from IHeatMeter hm " +
				"where hm.projectID='"+projectUID+"' " +
				"and hm.communityID='"+communityUID+"' " +
				"and hm.meterNo not in("+alreadyConfigHql+")";
		//热表号动态查询
		if(meterNo!=null && !"".equals(meterNo.trim())) {
			possibleConfigHql += " and lower(hm.meterNo) like '%"+meterNo.trim().toLowerCase()+"%'";
		}
		//结果集个数查询
		String countQueryHql = "select count(*) "+possibleConfigHql;
		int total = ((Long)this.getCurrentSession().createQuery(countQueryHql).uniqueResult()).intValue();
		//结果集排序
		possibleConfigHql += " order by hm.meterNo asc";
		//结果集分页
		Query listQuery = this.getCurrentSession().createQuery(possibleConfigHql);
		listQuery.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//封装查询结果
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatMeter> meterPage = new ResultWithPageCond<IHeatMeter>();
		meterPage.setList(listQuery.list());
		meterPage.setPageCond(pageCond);
		return meterPage;
	}
	
	/**
	 * 通过楼栋与热量表的关联关系查询配置项
	 * @param buildingUID
	 * @param meterNo
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-24
	 */
	public IBuildingConsumeConfig queryConfigByRelation(String buildingUID, 
			String meterNo) throws Exception {
		String hsql = "from IBuildingConsumeConfig bcc " +
				"where bcc.buildingID='"+buildingUID+"' " +
				"and bcc.meterNo='"+meterNo+"'";
		List<IBuildingConsumeConfig> configList = this.runQueryHSql(hsql);
		if(configList!=null && configList.size()>0) {
			return configList.get(0);
		}
		return null;
	}
	
	/**
	 * 通过楼栋与热量表的关联关系删除配置项
	 * @param buildingUID
	 * @param meterNo
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-24
	 */
	public ResponseMsg deleteConfigByRelation(String buildingUID, 
			String meterNo) throws Exception {
		String hsql = "delete from IBuildingConsumeConfig bcc " +
				"where bcc.buildingID='"+buildingUID+"' " +
				"and bcc.meterNo='"+meterNo+"'";
		this.runFormatHSql(hsql);
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 统计楼栋配置的计量热表个数
	 * @param buildingUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-28
	 */
	public int countBuildingMeasureMeterNum(String buildingUID) throws Exception {
		String hsql = "select count(*) from IBuildingConsumeConfig bcc " +
				"where bcc.buildingID='"+buildingUID+"'";
		return ((Long)this.getCurrentSession().createQuery(hsql).uniqueResult()).intValue();
	}
	
	/**
	 * 根据楼栋信息查询对应的计量热表
	 * @param buildingUID
	 * @param calculateType 热表计算关系
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-28
	 */
	@SuppressWarnings("unchecked")
	public List<IHeatMeter> queryMeasureMetersByBuildingUID(String buildingUID, 
			String calculateType) throws Exception {
		//根据楼栋耗热配置查询对应的计量热表表号
		String meterNoQueryHql = "select bcc.meterNo from IBuildingConsumeConfig bcc " +
				"where bcc.buildingID='"+buildingUID+"'";
		if(calculateType!=null && !"".equals(calculateType)) {
			meterNoQueryHql += " and bcc.calculateType='"+calculateType+"'";
		}
		//查询计量热表
		String hsql = "from IHeatMeter hm where hm.meterNo in("+meterNoQueryHql+")";
		return this.getCurrentSession().createQuery(hsql).list();
	}

	/**
	 * @return the iResourceAccessControlDao
	 */
	public IResourceAccessControlDao getiResourceAccessControlDao() {
		return iResourceAccessControlDao;
	}

	/**
	 * @param iResourceAccessControlDao the iResourceAccessControlDao to set
	 */
	public void setiResourceAccessControlDao(
			IResourceAccessControlDao iResourceAccessControlDao) {
		this.iResourceAccessControlDao = iResourceAccessControlDao;
	}
	
}
